package com.zhentao.interceptor;

import com.zhentao.model.common.manger.pojo.MangerUser;
import com.zhentao.utils.WmThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class WmTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        获取head里面的信息
        String userId = request.getHeader("mangerId");
        if(userId!=null){
//           把userId放到threadLocal里面
            MangerUser mangerUser = new MangerUser();
            mangerUser.setId(Integer.valueOf(userId));
            WmThreadLocalUtil.set(mangerUser);
            log.info("把用户信息放到了ThreadLocal里面:");
            log.info("获取用户信息", WmThreadLocalUtil.get());
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
